<template>
  <el-container class="container">
    <!-- 头部区 -->
    <el-header class="headers" height="80px">
      <img
        src="../assets/p01-1.jpg"
        alt=""
        style="height: 100%; border-radius: 50%"
      />
      <span>后台管理系统</span>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu active-text-color="#409FFF"  :collapse-transition="false" :unique-opened="true" :collapse="isCollapse" :default-active="activePath" :router="true">        
          <el-submenu :index="i.toString()" v-for="p,i in menulist" :key="p.id">
            <template slot="title">
                <!-- 一级菜单 -->
                <i :class="iconobj[p.id]"></i>
                <span slot="title">{{p.authName}}</span>
            </template>
            <!-- 二级菜单 -->
              <el-menu-item :index="'/'+item.path+''" v-for="item in p.children" :key="item.id" @click="saveNavState('/'+item.path)">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  {{item.authName}}
             </el-menu-item>

                   <!-- 三级菜单 -->
            <el-submenu index="1-10">        
              <template slot="title"><i class="el-icon-location"></i>待开发选项</template>
              <el-menu-item index="1-20">待开发</el-menu-item>
            </el-submenu>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-container>
        <!-- 主体区 -->
        <el-main>
            <!-- 路由占位符 -->
            <router-view></router-view>
        </el-main>

        
      </el-container>
    </el-container>
    <!-- 底部区 -->
        <el-footer>Footer</el-footer>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
      return {
        //左侧菜单数据
          menulist:[],
          iconobj:{
              "125":"el-icon-user",
              "103":"el-icon-s-tools",
              '101':"el-icon-goods",
              '102':"el-icon-document",
              '145':"el-icon-data-line"
          },
          isCollapse:false,
          //被激活的链接
          activePath:''
      }
  },
  created(){
      this.getMenuList()
      this.activePath=sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$message({
        message: "你已退出登录",
        type: "warning",
      });
      this.$router.push("/login");
    },
    //获取菜单栏
    async getMenuList(){
        const {data:res}=await this.$http.get('menus')
        if(res.meta.status!==200)return this.$message.error(res.meta.msg)
        this.menulist=res.data
        // console.log(res)
    },
    //菜单栏折叠与展开
    toggleCollapse(){
        this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath){
        //获取当前的url
        // const baseurl = window.location.href;
        // const a=baseurl.lastIndexOf('/')
        // const b=baseurl.length
        // const url=baseurl.substr(a)
        // const url=baseurl.substring(a)
        // console.log(url)
        
        sessionStorage.setItem('activePath',activePath)
        this.activePath=activePath
    }
  },
};
</script>

<style lang='less' scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
.iconfont{
    margin-right:7px;
}
.headers {
  height: 300px;
}
.el-header {
  background-color: #b3c0d1;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  font-weight: 800;
}
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
//   border-right: none;
  .el-menu{
    border-right: none;
  }
}
.toggle-button{
    background-color:#4A5064;
    color:white;
    text-align: center;
    line-height:24px;
    font-size: 10px;
    cursor: pointer;
    letter-spacing:0.3em;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  // text-align: center;
  // line-height: 160px;
}
.container {
}
.el-container {
  // margin-bottom: 40px;
  height: 100%;
}
</style>